package com.xilihui.xlh.business.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.EvaluateEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/24.
 */

public class FeedbackRecordActivity extends BaseCompatActivity implements View.OnClickListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ArrayList<EvaluateEntity.DataBean> datas = new ArrayList<>();
    BaseAdapter<EvaluateEntity.DataBean> beanBaseAdapter;
    
    ExceptionManager exceptionManager;
    
    int page = 1;
    
    
    @Override
    public int getLayout() {
        return R.layout.activity_feedback_record;
    }

    @Override
    public String initTitle() {
        return "评价记录";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
    
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_recored;
            }
        };
        
        exceptionManager.hide();
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        beanBaseAdapter = new BaseAdapter<EvaluateEntity.DataBean>(this,datas) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_evaluate;
            }

            @Override
            protected void getView(BaseViewHolder holder, EvaluateEntity.DataBean entity, int position) {
                
                String name = (String) SPUtil.get(FeedbackRecordActivity.this,SPUtil.NICK,""); 
                holder.setText(R.id.tv_name,name);
                String url = (String) SPUtil.get(FeedbackRecordActivity.this,SPUtil.HEAD_IMAGE,"");
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.civ_userHead);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+url);
                float num = Float.parseFloat(entity.getZan_num());
                holder.setRating(R.id.ratingBar,num);
                holder.setText(R.id.tv_fengshu,num+"分");
                holder.setText(R.id.tv_pingjia,entity.getContent());
                holder.setText(R.id.tv_shopName,entity.getName());
                holder.setText(R.id.tv_time,"预约时间："+entity.getStart_time()+"~"+entity.getEnd_time());
                holder.setText(R.id.tv_xianmu,"预约项目："+entity.getProject_name());
                holder.setText(R.id.tv_type,"座位类型："+entity.getSeat_name());
                
            }
        };
        recyclerView.setAdapter(beanBaseAdapter);
        
        getData(true,page);
        
    }

    @Override
    public void initData() {
        
    }
    
    public void getData(boolean isLoading, final int page){
        MyAppointmentRequest.evaluateList(this)
                .compose(DoTransform.<EvaluateEntity>applyScheduler(this,isLoading))
                .compose(FeedbackRecordActivity.this.<EvaluateEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<EvaluateEntity>(this) {
                    @Override
                    protected void onSuccess(EvaluateEntity entity) {
                       
                        if(page==1){
                            datas.clear();
                            if(entity.getData().size()<1){
                                exceptionManager.showEmpty();
                            }else {
                                exceptionManager.hide();
                            }
                        }
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();

                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        datas.addAll(entity.getData());
                        beanBaseAdapter.setList(datas);
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
                
    }

    @Override
    public void onClick(View view) {
        
    }
    
    
}
